#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Unit tests for lambda_algorithm.py
import lambda_algorithm
import unittest
import cPickle

class testLambdaAlgorithm(unittest.TestCase):
    
    def setUp(self):
        self.algorithm_ = lambda_algorithm.LambdaAlgorithm()
        
    def testPackages(self):
        self.algorithm_.packages_ = 100
        self.assertEqual(self.algorithm_.packages(), 100)
    
    def testFaults(self):
        self.algorithm_.faults_ = 100
        self.assertEqual(self.algorithm_.faults(), 100)
        

class testReplication(unittest.TestCase):
    
    def setUp(self):
        self.size_ = 5
        self.algorithm_ = lambda_algorithm.Replication(self.size_)
        
    def testLimits(self):
        self.assertEqual(self.algorithm_.packages(), self.size_)
        self.assertEqual(self.algorithm_.faults(), self.size_ - 1)

    def testDisperse(self):
        state = ['TEST STATE', 'RANDOM INFO']
        self.assertEqual(self.algorithm_.Disperse(state),
                         [cPickle.dumps(state)] * self.size_)

class testSingleParity(unittest.TestCase):
    
    def setUp(self):
        self.size_ = 4
        self.algorithm_ = lambda_algorithm.SingleParity(self.size_)
        
    def testLimits(self):
        self.assertEqual(self.algorithm_.packages(), self.size_)
        self.assertEqual(self.algorithm_.faults(), 1)

    def testDisperse(self):
        state = ['TEST STATE', 'RANDOM INFO ']
        self.assertEqual(self.algorithm_.Disperse(state),
                         ["S00000000000015(lp1\nS'TEST STA", 
                          "S00010000000015TE'\np2\naS'RANDO", 
                          "S00020000000014M INFO '\np3\na.x", 
                          'P000300000000141\t\x1eu<.\r\x12\x1c\x045k|>v'])
                      
        
if __name__ == '__main__':
    unittest.main()
